<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/9.png" />
        <span>小猪家的管账系统</span>
      </div>
      <div>
        <span style="margin-right: 30px">{{ username }} </span>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '180px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>

        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          router
        >
          <el-menu-item
            v-for="item in menulist"
            :key="item.id"
            :index="item.path"
            @select="saveNavState('/' + item.path)"
          >
            <i :class="iconObj[item.menuName]"></i>
            <span>{{ item.menuName }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 主题区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        首页: 'iconfont simple-zhexiantu',
        存款: 'iconfont simple-qiandai',
        生活: 'iconfont simple-dixiaofei',
        基金: 'iconfont simple-jijin',
        收入: 'iconfont simple-shouru',
        银行卡: 'iconfont simple-yinxingqia',
        猪猪: 'iconfont icon-user'
      },
      // 被激活的链接地址
      activePath: '',
      // 是否折叠
      isCollapse: false,
      username: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')

    this.username = window.sessionStorage.getItem('token') === 3 ? '园' : '川'
  },

  methods: {
    logout() {
      // 清除token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('/home/getMenus')
      if (res.code !== 1) {
        return this.$message.error(res.message)
      }
      this.menulist = res.data
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 点击按钮，切换菜单的折叠或展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.iconfont {
  margin-right: 10px;
}

.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
